package com.yh.autobio.common;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import com.dtr.zxing.activity.CaptureActivity;
import com.yh.autobio.interf.ICallbackResult;
import com.yh.autobio.modules.bean.CKInfo;
import com.yh.autobio.modules.bean.SimpleBackPage;
import com.yh.autobio.modules.service.DownloadService;
import com.yh.autobio.modules.ui.SimpleBackActivity;

import java.util.ArrayList;

/**
 * 界面帮助类
 * 
 * @author FireAnt（http://my.oschina.net/LittleDY）
 * @version 创建时间：2014年10月10日 下午3:33:36
 * 
 */
public class UIHelper {

    /** 全局web样式 */
    // 链接样式文件，代码块高亮的处理
    public final static String linkCss = "<script type=\"text/javascript\" src=\"file:///android_asset/shCore.js\"></script>"
            + "<script type=\"text/javascript\" src=\"file:///android_asset/brush.js\"></script>"
            + "<script type=\"text/javascript\" src=\"file:///android_asset/client.js\"></script>"
            + "<script type=\"text/javascript\" src=\"file:///android_asset/detail_page.js\"></script>"
            + "<script type=\"text/javascript\">SyntaxHighlighter.all();</script>"
            + "<script type=\"text/javascript\">function showImagePreview(var url){window.location.url= url;}</script>"
            + "<link rel=\"stylesheet\" type=\"text/css\" href=\"file:///android_asset/shThemeDefault.css\">"
            + "<link rel=\"stylesheet\" type=\"text/css\" href=\"file:///android_asset/shCore.css\">"
            + "<link rel=\"stylesheet\" type=\"text/css\" href=\"file:///android_asset/css/common.css\">";
    public final static String WEB_STYLE = linkCss;

    public static final String WEB_LOAD_IMAGES = "<script type=\"text/javascript\"> var allImgUrls = getAllImgSrc(document.body.innerHTML);</script>";

    private static final String SHOWIMAGE = "ima-api:action=showImage&data=";
    /**
     * 显示扫一扫界面
     *
     * @param context
     */
    public static void showScanActivity(Context context,String workno, ArrayList<CKInfo> checkedCKS,String title) {
        Bundle args=new Bundle();
        args.putString("workno",workno);
        args.putSerializable("checkedCKS",checkedCKS);
        Intent intent = new Intent(context, CaptureActivity.class);
        intent.putExtras(args);
        context.startActivity(intent);

//        Bundle args=new Bundle();
//        args.putString("workno",workno);
//        args.putSerializable("checkedCKS",checkedCKS);
//        showSimpleBack(context, SimpleBackPage.WORK_DETAIL_NEW,args,title);
    }
    //显示工序
    public static void showWorks(Context context,String workno) {
        Bundle args=new Bundle();
        args.putString("workno",workno);
        String title="派工序号"+workno;
        showSimpleBack(context, SimpleBackPage.WORK_DETAIL,args,title);
    }

    /**
     * 显示关于界面
     *
     * @param context
     */
    public static void showAboutOSC(Context context) {
        showSimpleBack(context, SimpleBackPage.ABOUT_ZWS,null,"关于");
    }

    public static void openDownLoadService(Context context, String downurl,
                                           String tilte) {
        final ICallbackResult callback = new ICallbackResult() {

            @Override
            public void OnBackResult(Object s) {}
        };
        ServiceConnection conn = new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {}

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                DownloadService.DownloadBinder binder = (DownloadService.DownloadBinder) service;
                binder.addCallback(callback);
                binder.start();

            }
        };
        Intent intent = new Intent(context, DownloadService.class);
        intent.putExtra(DownloadService.BUNDLE_KEY_DOWNLOAD_URL, downurl);
        intent.putExtra(DownloadService.BUNDLE_KEY_TITLE, tilte);
        context.startService(intent);
        context.bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }
    public static void showSimpleBackForResult(Fragment fragment, FragmentActivity activity,
                                               int requestCode, SimpleBackPage page, Bundle args, String disp) {
        Intent intent = new Intent(activity,
                SimpleBackActivity.class);
        intent.putExtra(SimpleBackActivity.BUNDLE_KEY_PAGE, page.getValue());
        intent.putExtra(SimpleBackActivity.BUNDLE_KEY_ARGS, args);
        intent.putExtra(SimpleBackActivity.BUNDLE_KEY_DISPLAY_TYPE_STRING, disp);
        fragment.startActivityForResult(intent, requestCode);
    }
    public static void showSimpleBack(Context context, SimpleBackPage page,
                                      Bundle args, String disp) {
        Intent intent = new Intent(context, SimpleBackActivity.class);
        intent.putExtra(SimpleBackActivity.BUNDLE_KEY_ARGS, args);
        intent.putExtra(SimpleBackActivity.BUNDLE_KEY_DISPLAY_TYPE_STRING, disp);
        intent.putExtra(SimpleBackActivity.TYPE, "str");
        intent.putExtra(SimpleBackActivity.BUNDLE_KEY_PAGE, page.getValue());
        context.startActivity(intent);
    }

    public static void showSalaryReport(Context mContext, String title, String startDate,String endDate) {
        Bundle args=new Bundle();
        args.putString("startDate",startDate);
        args.putString("endDate",endDate);
        showSimpleBack(mContext, SimpleBackPage.SALARYREPORT,args,title);
    }
}
